################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

## Last updated: May 12th, 2025

## Description:

# This master script sets the working directory to ./ReplicationPackage/scripts,
# and all file paths in the analysis are defined relative to this location.
# It provides a sequential overview of the scripts required to fully replicate the analysis.
#
# To run the complete analysis, ensure that all necessary R packages are installed.
# A full list of required packages is provided in the README.rtf under the section
# titled "Software and packages." 
#
# The scripts below will load the required packages into the R session.

################################################################################



rm(list = ls())
gc()

# Set wd using Rstudio (the WD should be set to the script folder of the Rep pack.)
current_path = rstudioapi::getActiveDocumentContext()$path
setwd(dirname(current_path))

################################################################################

############ 0_data_creation ###################################################

################################################################################



################## 0_1 Pre-processing #########################################

source("./0_data_creation/1_pre_processing/gen_data.R")

############## 0_2 Generate document term matrix ###############################

source("./0_data_creation/2_generate_dtm/generate_dtm.R")

############## 0_3 Run distributed multinomial regression (dmr) ################

# estimates - divergence across the word propensity (q), posterior (rho) and divergence (pi)
# for all characteristics

### Bloc

source("./0_data_creation/3_generate_q_rho_pi/1_0_bloc.R")
source("./0_data_creation/3_generate_q_rho_pi/1_1_bloc_placebo.R")


### Gender
source("./0_data_creation/3_generate_q_rho_pi/2_0_gender.R")
source("./0_data_creation/3_generate_q_rho_pi/2_1_gender_placebo.R")


### Age

source("./0_data_creation/3_generate_q_rho_pi/3_0_age.R")
source("./0_data_creation/3_generate_q_rho_pi/3_1_age_placebo.R")


### Town

source("./0_data_creation/3_generate_q_rho_pi/4_0_town.R")
source("./0_data_creation/3_generate_q_rho_pi/4_1_town_placebo.R")


### Occupation

source("./0_data_creation/3_generate_q_rho_pi/5_0_occupation.R")
source("./0_data_creation/3_generate_q_rho_pi/5_1_occupation_placebo.R")


### Appendix

# ---------------------------
# Gender
# ---------------------------

source("./0_data_creation/3_generate_q_rho_pi/appendix/1_0_gender_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/1_1_gender_placebo_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/5_0_gender_party.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/5_1_gender_placebo_party.R")

# ---------------------------
# Age
# ---------------------------
source("./0_data_creation/3_generate_q_rho_pi/appendix/2_0_age_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/2_1_age_placebo_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/6_0_age_party.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/6_1_age_placebo_party.R")

# ---------------------------
# Town
# ---------------------------
source("./0_data_creation/3_generate_q_rho_pi/appendix/3_0_town_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/3_1_town_placebo_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/7_0_town_party.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/7_1_town_placebo_party.R")

# ---------------------------
# Occupation
# ---------------------------
source("./0_data_creation/3_generate_q_rho_pi/appendix/4_0_occupation_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/4_1_occupation_placebo_com.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/8_0_occupation_party.R")
source("./0_data_creation/3_generate_q_rho_pi/appendix/8_1_occupation_placebo_party.R")

################### 04 divergent words #############################################

source("./0_data_creation/4_divergent_words/zeta_top_50.R")
# Can be run by reticulate, but probably easier to run manually, and not from master.R
library(reticulate)
#virtualenv_create("r-reticulate") # Create  "r-reticulate", if not already created
use_virtualenv("r-reticulate") # Use virtualeenv
py_install("googletrans==4.0.0-rc1") # install, if not already installed
py_install("pandas") # install, if not already installed
source_python("./0_data_creation/4_divergent_words/zeta_translate_top50.py")

################### 05 Magnitudes #############################################

source("./0_data_creation/5_monte_carlo_magnitude/monte_carlo_magnitude_pi.R")


################################################################################

############ 1_table_scripts (they have to be run before figures, as they create
# some summary statistics used in figures) ####################################

################################################################################


# Tables in paper
source("./1_table_scripts/tab1.R")

# Appendix tables
source("./1_table_scripts/tabA3_and_A5.R")
source("./1_table_scripts/tabA4.R")
source("./1_table_scripts/tabA6.R")
source("./1_table_scripts/tabA7.R")
source("./1_table_scripts/tabA8.R")
source("./1_table_scripts/tabA9.R")
source("./1_table_scripts/tabA10.R")
source("./1_table_scripts/tabA11_and_A12.R")
source("./1_table_scripts/tabC1.R")

################################################################################

############ 2_figure_scripts ####################################

################################################################################

# Figures in paper


source("./2_figure_scripts/fig1.R")
source("./2_figure_scripts/fig2a.R")
source("./2_figure_scripts/fig2b.R")
source("./2_figure_scripts/fig3.R")
source("./2_figure_scripts/fig4.R")

# Appendix figures

source("./2_figure_scripts/figA1.R")
source("./2_figure_scripts/figA2.R")
source("./2_figure_scripts/figA3.R")
source("./2_figure_scripts/figA4.R")
source("./2_figure_scripts/figA5.R")
source("./2_figure_scripts/figA6.R")
source("./2_figure_scripts/figA7.R")
source("./2_figure_scripts/figA8.R")
source("./2_figure_scripts/figA9.R")
source("./2_figure_scripts/figA10.R")
source("./2_figure_scripts/figA11.R")
source("./2_figure_scripts/figC1.R")























